/**
 * Created by George on 2018/1/27.
 * 用户列表
 */
define(['angular', 'util'], function (angular, util) {
    "use strict";
    // 将本控制器函数作为结果返回给router.js
    var userCtrl = function ($scope, $http) {
        $scope.welcome_info = "会员管理";
        $scope.userCount = 0;
        $scope.memberListDatas = [];
        $scope.dues = {
            consume: 0,
            realConsume: 0,
            leave: 0
        };

        function initOrUpdateData() {
            $scope.dues = {
                consume: 0,
                realConsume: 0,
                leave: 0
            };

            $http.get("/basketballfire/rest/user/list").then(function (data, status) {
                console.log(data);
                var usersData = data.data.data;
                initUsersData(usersData);
            });
        }

        function init() {
            initOrUpdateData();
        }

        function initUsersData(usersData) {
            $scope.userCount = usersData.length;
            $scope.dues.leave = 0;
            var index;
            for (index = 0; index < usersData.length; index++) {
                $scope.dues.leave = util.add($scope.dues.leave, usersData[index].dues);
            }

            $scope.memberListDatas = [];
            for (index = 0; index < usersData.length;) {
                var data = {
                    left: {},
                    right: {}
                };
                data.left = usersData[index];
                if (index + 1 < usersData.length) {
                    data.right = usersData[index + 1];
                }
                $scope.memberListDatas.push(data);
                index += 2;
            }
        }

        //ctrl start
        init();
    };

    return userCtrl;
});